﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using System.Web;
using Microsoft.Office.Server.SocialData;
using Microsoft.SharePoint.Linq;
using System.Linq;
using System.Data;
using System.Text;
using Square.Activity;

namespace Square.OverView.SquareMemo
{
    public partial class SquareMemoUserControl : OverViewBaseControl
    {   
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                MaxItem.Value = "5";
                RecordIDorUrl();
                TodayAdd();
            } 
           
            SquareActivities Atype = new SquareActivities();
            Atype = SquareActivities.MemoAll;
            ActViewer.SquareActivityType = Atype;
        }

        private void TodayAdd()
        {
            using (SPSite _site = new SPSite(SPContext.Current.Site.ID))
            {
                
                using (SPWeb _web = _site.OpenWeb())
                {
                    _web.AllowUnsafeUpdates = true;
                    string thisUrl = HttpUtility.UrlDecode(Request.Url.AbsoluteUri.Split('&')[0]);
                    string[] Arrurl = null;
                    SPList _list = _web.Lists["MyprofileToday"];
                    //SPListItem spitem = _list.Items.Add();
                    if (HttpUtility.UrlDecode(thisUrl).Contains("accountname"))
                    {
                        Arrurl = thisUrl.Split('=');

                        if (TodayCheck(Arrurl[1]) == 0)
                        {
                            if (Arrurl[1] != HttpContext.Current.User.Identity.Name)
                            {

                                CurrentUserId = Arrurl[1];
                                string strFolderUrl = _list.RootFolder.ServerRelativeUrl + "/" + CurrentUserId;

                                //if (SearchFolder(_list, myUserProfile.RecordId.ToString()) == 0)
                                //{
                                try
                                {
                                    //목록폴더를 생성한다
                                    SPListItem NewFolder = _list.Items.Add("", SPFileSystemObjectType.Folder, CurrentUserId);
                                    //NewFolder["Group_ID"] = Pramitems[2];
                                    NewFolder.Update();
                                }
                                catch { }
                                //}

                                SPListItem SpItem = _list.Items.Add(strFolderUrl, SPFileSystemObjectType.File, null);
                                SpItem["Title"] = CurrentUserId;
                                SpItem["UserID"] = HttpContext.Current.User.Identity.Name;
                                SpItem.Update();
                            }
                        }
                    }
                    _web.AllowUnsafeUpdates = false;
                }
                
            }
        }

        private int TodayCheck(string Userid)
        {
            int TodayCount = 0;
            CurrentUserId = Userid;
            using (SPSite _site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb _web = _site.OpenWeb())
                {                    
                    SPList _list = _web.Lists["MyprofileToday"];
                    var Replyitems = from SPListItem spitem in _list.Items
                                     where spitem.Title == CurrentUserId && spitem["UserID"].ToString() == HttpContext.Current.User.Identity.Name && spitem["Created"].ToString().Substring(0,10) == DateTime.Now.ToShortDateString()
                                     select spitem;
                    TodayCount = Replyitems.Count();
                }
            }
            return TodayCount;
        }

        #region DataBind
        //Profile Page Check
        public void RecordIDorUrl()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);

                if (HttpUtility.UrlDecode(Request.Url.AbsoluteUri.Split('&')[0]).Contains(myUserProfile.DisplayName))
                {
                    phMyfiltering.Visible = true;
                    phOtherfiltering.Visible = false;
                    RecordIDComments();
                }

                else if (!(Request.Url.AbsoluteUri.Split('&')[0].Contains("accountname")))
                {
                    phMyfiltering.Visible = true;
                    phOtherfiltering.Visible = false;
                    RecordIDComments();
                }
                else
                {
                    phMyfiltering.Visible = false;
                    phOtherfiltering.Visible = true;
                    UrlComments();
                }
            }
            AddListVisible();
        }

        //Use RecordID
        public void RecordIDComments()
        {
            Literal1.Text = "";
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager myUserProfileManager = new UserProfileManager(context);
                    //UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
                    SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);


                    UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);

                    //SocialComment[] comments = mySocialCommentManager.GetComments(myUserProfile);

                    DataSet ds = new DataSet();
                    ds = EditGetComments(myUserProfile, Convert.ToInt32(MaxItem.Value));
                    //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    //non-public member안의 property안 partitionid
                    //myUserProfile.Suggestions.GetType().GetProperty("PartitionID", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(myUserProfile.Suggestions, null).ToString();

                    //non-public member안의 field안 값
                    //myUserProfile.Suggestions.GetType().GetField("PartitionID", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(myUserProfile.Suggestions).ToString();
                    //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    hfCommentCount.Value = ds.Tables[0].Rows.Count.ToString();
                    rtMemoList.DataSource = ds;
                    rtMemoList.DataBind();

                });
            }
        }

        //Use Url
        public void UrlComments()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    DataSet ds = new DataSet();
                    ds = EditGetUrlComments(HttpUtility.UrlDecode(Request.Url.AbsoluteUri.Split('&')[0]), Convert.ToInt32(MaxItem.Value));

                    hfCommentCount.Value = ds.Tables[0].Rows.Count.ToString();
                    rtMemoList.DataSource = ds;
                    rtMemoList.DataBind();
                    hfnowstate.Value = "otherall";
                    AddTabListVisible(Convert.ToInt64(ds.Tables[1].Rows[0]["Total"].ToString()));
                });
            }
        }

        protected void Parent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            hfcommentid.Value = DataBinder.Eval(e.Item.DataItem, "CommentID").ToString();
            OnLoadReply(hfcommentid.Value, e);
        }

        private void OnLoadReply(string commentID, RepeaterItemEventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList _list = web.Lists["MemoReplyList"];

                    var Replyitems = from SPListItem spitem in _list.Items
                                     where spitem["CommentID"].ToString() == commentID
                                     select new
                                     {
                                         Url = spitem["Url"].ToString()
                                         ,
                                         UserName = spitem["UserName"].ToString()
                                         ,
                                         ReplyComment = spitem["ReplyComment"].ToString()
                                         ,
                                         RecordID = spitem["RecordID"].ToString()
                                         ,
                                         ID = spitem["ID"].ToString()
                                         ,
                                         Created = spitem["Created"].ToString()
                                     };

                    //SPQuery query = new SPQuery();
                    //StringBuilder strQuery = new StringBuilder();
                    //strQuery.Append("<OrderBy>");
                    //strQuery.Append("<FieldRef Name=\"Created\" Ascending=\"False\" />");
                    //strQuery.Append("</OrderBy>");
                    //strQuery.AppendLine("<Where>");
                    //strQuery.AppendLine("<Eq>");
                    //strQuery.AppendLine("<FieldRef Name=\"CommentID\" />");
                    //strQuery.AppendFormat("<Value Type=\"Text\" >{0}</Value>", commentID);
                    //strQuery.AppendLine("</Eq>");
                    //strQuery.AppendLine("</Where>");

                    //query.Query = strQuery.ToString();
                    //query.RowLimit = Convert.ToUInt32(_list.ItemCount);                   

                    if (Replyitems != null)
                    {
                        if (Replyitems.Count() > 0)
                        {
                            Repeater childRepeater = (Repeater)e.Item.FindControl("rtReply");
                            childRepeater.DataSource = Replyitems;
                            childRepeater.DataBind();
                        }
                    }
                }
            }
        }

        protected void lbAllData_Click(object sender, EventArgs e)
        {
            RecordIDorUrl();
        }

        protected void lbMyData_Click(object sender, EventArgs e)
        {
            MyDataBind();
        }

        private void MyDataBind()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager myUserProfileManager = new UserProfileManager(context);
                    //UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
                    SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                    UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);

                    DataSet ds = new DataSet();
                    ds = GetMyComments(myUserProfile, Convert.ToInt32(MaxItem.Value));

                    hfCommentCount.Value = ds.Tables[0].Rows.Count.ToString();
                    rtMemoList.DataSource = ds;
                    rtMemoList.DataBind();

                    AddTabListVisible(Convert.ToInt64(ds.Tables[1].Rows[0]["Total"].ToString()));
                });
            }
        }

        protected void lbShareData_Click(object sender, EventArgs e)
        {
            ShareDataBind();
        }

        private void ShareDataBind()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager myUserProfileManager = new UserProfileManager(context);
                    //UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
                    SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                    UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);

                    DataSet ds = new DataSet();
                    ds = GetShareComments(myUserProfile, Convert.ToInt32(MaxItem.Value));

                    hfCommentCount.Value = ds.Tables[0].Rows.Count.ToString();
                    rtMemoList.DataSource = ds;
                    rtMemoList.DataBind();
                    AddTabListVisible(Convert.ToInt64(ds.Tables[1].Rows[0]["Total"].ToString()));
                });
            }
        }

        protected void lbOtherAllData_Click(object sender, EventArgs e)
        {
            UrlComments();
        }

        protected void lbOwnerData_Click(object sender, EventArgs e)
        {
            OwnerDataBind();
        }

        private void OwnerDataBind()
        {
            string OwnerUrl = string.Empty;
            string[] Arrowner = null;
            string OwnerID = string.Empty;
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    OwnerUrl = HttpUtility.UrlDecode(Request.Url.AbsoluteUri.Split('&')[0]);
                    OwnerID = OwnerUrl.Substring(OwnerUrl.IndexOf("accountname=") + 12);

                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager myUserProfileManager = new UserProfileManager(context);
                    //UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
                    SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                    UserProfile myUserProfile = myUserProfileManager.GetUserProfile(OwnerID);

                    DataSet ds = new DataSet();
                    ds = GetMyComments(myUserProfile, Convert.ToInt32(MaxItem.Value));

                    hfCommentCount.Value = ds.Tables[0].Rows.Count.ToString();
                    rtMemoList.DataSource = ds;
                    rtMemoList.DataBind();

                    AddTabListVisible(Convert.ToInt64(ds.Tables[1].Rows[0]["Total"].ToString()));
                });
            }
        }
        #endregion

        protected void btnMore_Click(object sender, EventArgs e)
        {
            MaxItem.Value = (Convert.ToInt32(MaxItem.Value) + 5).ToString();

            switch (hfnowstate.Value)
            {
                case "my":
                    MyDataBind();
                    break;

                case "share":
                    ShareDataBind();
                    break;

                case "otherall":
                    UrlComments();
                    break;

                case "owner":
                    OwnerDataBind();
                    break;

                default:
                    RecordIDorUrl();                    
                    break;
            }
        }

        public void AddListVisible()
        {
            double CommentsCount = 0;
            double DisplayCommentCount = 0;

            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
                DisplayCommentCount = Convert.ToInt64(hfCommentCount.Value);
                CommentsCount = EditGetCommentsCount(myUserProfile);

                if (DisplayCommentCount > CommentsCount)
                {
                    btnMore.Visible = false;
                    divMore.Visible = false;
                }

                else
                {
                    btnMore.Visible = true;
                    divMore.Visible = true;
                }
            }
        }

        public void AddTabListVisible(double Count)
        {            
            double DisplayCommentCount = 0;


            DisplayCommentCount = Convert.ToInt64(hfCommentCount.Value);

            if (DisplayCommentCount >= Count)
            {
                btnMore.Visible = false;
                divMore.Visible = false;
            }

            else
            {
                btnMore.Visible = true;
                divMore.Visible = false;
            }
        }

        #region Save
        //Comment Save
        protected void MemoSave_Click(object sender, EventArgs e)
        {
            string strActivity = string.Empty;
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
                string Serializedurl = string.Empty;
                string[] ArrSerializedurl = new string[2];
                string ActivityUrl = string.Empty;

                Serializedurl = HttpUtility.UrlDecode(GetEditSerializedUrl(new Uri(Request.Url.AbsoluteUri.Split('&')[0])).AbsoluteUri);

                if (Serializedurl.Contains("="))
                {
                    ArrSerializedurl = Serializedurl.Split('=');
                    Serializedurl = ArrSerializedurl[0] + "=" + ArrSerializedurl[1].Replace("\\", "%5C").Replace(".", "%2E").Replace("\\\\", "%5C");
                    if (HttpContext.Current.User.Identity.Name == HttpUtility.UrlDecode(ArrSerializedurl[1]))
                        strActivity = " Note was registered.";
                    else
                    {
                        CurrentUserId = HttpUtility.UrlDecode(ArrSerializedurl[1]);
                        strActivity = string.Format(" Has registered a note to <a href='{0}'>{1}</a>'s.", Serializedurl, CurrentUserId);
                    }
                }
                else
                {
                    Serializedurl = myUserProfile.PublicUrl.AbsoluteUri;
                    strActivity = " Note was registered.";
                }

                Uri myUri = new Uri(Serializedurl);

                mySocialCommentManager.AddComment(myUri, txtMemo.Text, false, myUserProfile.DisplayName);

                ActivityUrl = HttpUtility.UrlDecode(Serializedurl);

                bool bSave = false;
                SquareActivities aType = new SquareActivities();

                aType = SquareActivities.SquareMemoWrite;
                SquareActivityHelper sHelper = new SquareActivityHelper();
                bSave = sHelper.SaveActivity(this.Page, aType, strActivity + string.Format(" \"{0}\"",txtMemo.Text.Length > 24 ? txtMemo.Text.Substring(0,24)+" ......" : txtMemo.Text));
                
                RecordIDorUrl();
                txtMemo.Text = "";
            }
        }

        //ReplyComment Save
        protected void btnrply_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList _list = web.Lists["MemoReplyList"];
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                    UserProfileManager myUserProfileManager = new UserProfileManager(context);
                    UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
                    string Serializedurl = string.Empty;
                    string[] ArrSerializedurl = new string[2];
                    string ActivityUrl = string.Empty;
                    string strActivity = string.Empty;
                    string strFolderUrl = _list.RootFolder.ServerRelativeUrl + "/" + myUserProfile.RecordId.ToString();

                    //if (SearchFolder(_list, myUserProfile.RecordId.ToString()) == 0)
                    //{
                    try
                    {
                        //목록폴더를 생성한다
                        SPListItem NewFolder = _list.Items.Add("", SPFileSystemObjectType.Folder, myUserProfile.RecordId.ToString());
                        //NewFolder["Group_ID"] = Pramitems[2];
                        NewFolder.Update();
                    }
                    catch { }
                    //}

                    SPListItem SpItem = _list.Items.Add(strFolderUrl, SPFileSystemObjectType.File, null);


                    SpItem["CommentID"] = hfcommentid.Value;
                    SpItem["RecordID"] = myUserProfile.RecordId;
                    SpItem["Url"] = myUserProfile.PublicUrl.AbsoluteUri;
                    SpItem["UserName"] = myUserProfile.DisplayName;
                    SpItem["Title"] = myUserProfile.RecordId;
                    SpItem["ReplyComment"] = hfReply.Value;
                    SpItem["Depth"] = "1";
                    SpItem.Update();

                    Serializedurl = HttpUtility.UrlDecode(GetEditSerializedUrl(new Uri(Request.Url.AbsoluteUri.Split('&')[0])).AbsoluteUri);

                    if (Serializedurl.Contains("="))
                    {
                        ArrSerializedurl = Serializedurl.Split('=');
                        Serializedurl = ArrSerializedurl[0] + "=" + ArrSerializedurl[1].Replace("\\", "%5C").Replace(".", "%2E").Replace("\\\\", "%5C");
                        if (myUserProfile.RecordId.ToString() == hfrecordid.Value)
                            strActivity = string.Format(" \"{0}\" Comment have been registered to ", hfReply.Value.Length > 10 ? hfReply.Value.Substring(0, 10) + " ......" : hfReply.Value);
                        else
                        {
                            UserProfile ReplyProfile = myUserProfileManager.GetUserProfile(long.Parse(hfrecordid.Value));
                            CurrentUserId = ReplyProfile.DisplayName;
                            strActivity = string.Format(" \"{2}\" comment have been registered to <a href='{0}'>{1}</a> ", ReplyProfile.PublicUrl.AbsoluteUri, CurrentUserId, hfReply.Value.Length > 10 ? hfReply.Value.Substring(0, 10) + " ......" : hfReply.Value);
                        }
                    }
                    else
                    {
                        Serializedurl = myUserProfile.PublicUrl.AbsoluteUri;

                        if (myUserProfile.RecordId.ToString() == hfrecordid.Value)
                            strActivity = string.Format(" \"{0}\" Comment have been registered to", hfReply.Value.Length > 10 ? hfReply.Value.Substring(0, 10) + " ......" : hfReply.Value);
                        else
                        {
                            UserProfile ReplyProfile = myUserProfileManager.GetUserProfile(long.Parse(hfrecordid.Value));
                            CurrentUserId = myUserProfile.DisplayName;
                            strActivity = string.Format(" \"{2}\" comment have been registered to <a href='{0}'>{1}</a>'s", ReplyProfile.PublicUrl.AbsoluteUri, CurrentUserId, hfReply.Value.Length > 10 ? hfReply.Value.Substring(0, 10) + " ......" : hfReply.Value);
                        }

                        strActivity = string.Format(" \"{0}\" Comment have been registered to ", hfReply.Value.Length > 10 ? hfReply.Value.Substring(0, 10) + " ......" : hfReply.Value);
                    }

                    //ActivityUrl = HttpUtility.UrlDecode(Serializedurl);

                    bool bSave = false;
                    SquareActivities aType = new SquareActivities();

                    aType = SquareActivities.SquareMemoCommentWrite;
                    SquareActivityHelper sHelper = new SquareActivityHelper();
                    bSave = sHelper.SaveActivity(this.Page, aType, strActivity + string.Format(" \"{0}\"", Strip(hfParentComment.Value).Length > 25 ? Strip(hfParentComment.Value).Substring(0, 25) + " ......" : Strip(hfParentComment.Value)));

                    RecordIDorUrl();
                    //OnLoadReply(hfcommentid.Value);
                }
            }
        }

        //ShareComment Save
        protected void lbShareSave_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {

                SPServiceContext context = SPServiceContext.GetContext(site);
                SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
                Uri myUri = new Uri(myUserProfile.PublicUrl.AbsoluteUri);

                mySocialCommentManager.AddComment(myUri, "<img alt='' src='/_layouts/Square/Images/ic_favorite.png' title='Share Post' />" + hfrewrite.Value +" : "+hfShareComment.Value, false, "Share");

                bool bSave = false;
                SquareActivities aType = new SquareActivities();

                aType = SquareActivities.SquareMemoCommentWrite;
                SquareActivityHelper sHelper = new SquareActivityHelper();
                bSave = sHelper.SaveActivity(this.Page, aType, string.Format(" Share post was registered. \"{0} : {1}\"",hfrewrite.Value, hfShareComment.Value.Length > 15 ? hfShareComment.Value.Substring(0, 15) + " ......" : hfShareComment.Value));

                RecordIDorUrl();
            }
        }
        #endregion

        #region Delete
        protected void lbCommentDel_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
                try
                {
                    DBCommentDelete(myUserProfile, hfcommentid.Value);
                    ReplyCommentAllDel(hfcommentid.Value);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }

                RecordIDorUrl();
            }

        }

        private void ReplyCommentAllDel(string ParentCommentID)
        {
            using (SPSite _site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb _web = _site.OpenWeb())
                {
                    SPList _list = _web.Lists["MemoReplyList"];

                    var Replyitems = from SPListItem spitem in _list.Items
                                     where spitem["CommentID"].ToString() == ParentCommentID
                                     select spitem;

                    int itemcount = Replyitems.Count();
                    if (Replyitems != null)
                    {
                        for (int i = 0; i < itemcount; i++)
                        {
                            SPListItem item = Replyitems.First();
                            item.Delete();
                        }
                    }
                }
            }
        }

        protected void lbReplyCommentDel_Click(object sender, EventArgs e)
        {
            using (SPSite _site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb _web = _site.OpenWeb())
                {
                    SPList _list = _web.Lists["MemoReplyList"];

                    var Replyitems = from SPListItem spitem in _list.Items
                                     where spitem.Title == hfrerecordid.Value && spitem.ID.ToString() == hfrecommentid.Value
                                     select spitem;                    

                    SPListItem item = Replyitems.First();
                    item.Delete();
                    
                    RecordIDorUrl();
                }
            }
        }
        #endregion

        #region ##Return String
        protected string GetDispayName(string RecordID)
        {
            string strName = string.Empty;
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(Convert.ToInt64(RecordID));
                CurrentUserId = myUserProfile.DisplayName;
                strName = CurrentUserId;
            }
            return strName;
        }

        protected string GetReplyDispayName(string username)
        {
            string strname = string.Empty;

            CurrentUserId = username;
            strname = CurrentUserId;

            return strname;
        }

        protected string GetstrUrl(string RecordID)
        {
            string strUrl = string.Empty;
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(Convert.ToInt64(RecordID));
                strUrl = myUserProfile.PublicUrl.AbsoluteUri;
            }
            return strUrl;
        }

        protected string DelDisplay(string RecordID)
        {
            string Display = string.Empty;

            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);

                if (RecordID == myUserProfile.RecordId.ToString())
                    Display = "block";
                else
                    Display = "none";
            }
            return Display;
        }

        protected string DisplayImage(string recordid)
        {
            string ImageUrl = string.Empty;
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager myUserProfileManager = new UserProfileManager(context);
                UserProfile myUserProfile = myUserProfileManager.GetUserProfile(Convert.ToInt64(recordid));
                if (myUserProfile["PictureURL"].Value != null)
                {
                    if (myUserProfile["PictureURL"].ToString() == "")
                        ImageUrl = "/_layouts/images/O14_person_placeHolder_192.png";
                    else
                        ImageUrl = myUserProfile["PictureURL"].ToString();
                }
                else
                    ImageUrl = "/_layouts/images/O14_person_placeHolder_192.png";
            }
            return ImageUrl;
        }

        protected string GetDate(string strDate)
        {
            string rtDateState = string.Empty;
            if (DateTime.Now.ToShortDateString() == DateTime.Parse(strDate).ToShortDateString())
                rtDateState = "block";

            else
                rtDateState = "none";

            return rtDateState;
        }
        #endregion
    }
}
